<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

//引入数据库
use think\Db;

use think\Request;

class Recharge extends Backend {
    //默认配置

    public function __construct() {

        // 继承父类
        parent::__construct();

        // 引入模型
        $this->ConfigModel = model( 'Config' );
        $this->UserModel = model( 'User.User' );
        $this->RechargeModel = model( 'User.Recharge' );
    }

    // 获取数据信息

    public function index() {

        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'recharge.createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'recharge.createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'recharge.createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'nickname' ] = [ 'like', "%$keywords%" ];
        }

        $rechargeList = $this->RechargeModel->with( [ 'user', 'admin' ] )->where( $where )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );
        $rechargeStr = $rechargeList->render();

        // 查询帖子总数
        $rechargeCount = count( $rechargeList );

        $this->assign( [
            'rechargeList' => $rechargeList,
            'keywords' => $keywords,
            'rechargeCount' => $rechargeCount,
            'rechargeStr' => $rechargeStr,
            'start' => $start,
            'end' => $end,
        ] );

        return $this->fetch();

    }

    // 修改状态

    public function exam() {
        $payid = $this->request->param( 'ids', 0 );
        $content = $this->request->param( 'content', '' );
        $status = $this->request->param( 'status', '' );

        $payid = explode( ',', $payid );

        $list = [];

        foreach ( $payid as $item ) {
            $payinfo = $this->RechargeModel->where( [ 'id' => intval( $item ) ] )->find();

            if ( !$payinfo ) {
                $this->error( '充值记录不存在！' );
                exit;
            }

            $data = [
                'id' => intval( $item ),
                'content' => $content,
                'status' => $status,
            ];

            array_push( $list, $data );

        }

        if ( $status == '' ) {
            $this->error( '修改状态失败！' );
            exit;
        }

        $res = $this->RechargeModel->isUpdate()->saveAll( $list );

        // return $this->RechargeModel->getlastSql();

        if ( $res === false ) {
            $this->error( '修改状态失败！' );
            exit;
        } else {
            $this->success( '修改状态成功！' );
            exit;
        }

        return $list;
    }
}
?>